<?php


namespace app\common\server;

use app\index\controller\Common;

class OrderServer extends Common
{
    /**
     * 生成订单
     */
    public function addOrderServer($data)
    {
        $ca_id = $data['ca_id'];
        $us_id = $data['id'];                            // 用户id
        if (empty($data['goods_id'])) {
            $this->result('', '0', '请选择商品', 'json');
        }

        $good_id = $data['goods_id'];

//        // 判断商品数量 和赠品数量是否一致
//        foreach ($good_id as $k =>$v){
//            $gongId[] =  model('product')->where('id',$v)->value('type');
//            $num = array_count_values($gongId);
//            $num  = array_unique($num);
//            if (count($num)>1){
//                return ['code'=>0,'msg'=>'商品数量和赠品数量必须一致'];
//            }
//        }


         // 获取商品id,判断是否为同一个供货商
         // 商品id 根据商品id，判断是商品区还是赠品区

        if (empty($good_id)) {
            return ['code' => 0, 'msg' => '请选择商品'];
        }

        // 筛选出商品id
        $gongId = model('product')->whereIn('id', $good_id)->field('id,gong_id,ca_id,type')->select();
        foreach ($gongId as $k =>$v){
            if ($v['type'] == 1){ // 商品区
               $s_id[] = $v['id'];
            }
        }
        if (empty($s_id)){
            return ['code' => 0, 'msg' => '必须选择商品'];
        }
        foreach ($s_id as $k=>$v){
            $gid[] =  model('product')->where('id', $v)->value('gong_id');
        }
        $num = array_unique($gid);
        if (count($num) > 1) {
            return ['code' => 0, 'msg' => '请选择同一供货商商品'];
        }
        $gongIds= array_shift($num);




//        foreach ($gongId as $k => $v) {
//            $gongId[] = model('product')->where('id', $v)->field('gong_id,ca_id,type')->find();
//
//
//                if ($gongId['type'] == 1) {
//                    $gong_info[] = $gongId['gong_id'];
//                    $num = array_unique($gong_info);
//                    if (count($num) > 1) {
//                        return ['code' => 0, 'msg' => '请选择同一供货商商品'];
//                    }
//                } else {
//                    return ['code' => 0, 'msg' => '必须选择商品'];
//                }
////
////            if ($gongId['type'] == 1){
////                $gong_info[] = $gongId['gong_id'];
////                $num  = array_unique($gong_info);
////                if (count($num)>1){
////                    return ['code'=>0,'msg'=>'请选择同一供货商商品'];
////                }
////            }
////            else{
////                return ['code'=>0,'msg'=>'必须选择商品'];
////            }
//            }
//            halt($gongId);

            $order = [
                'us_id' => $us_id,         //用户id
                'or_status' => 0,          // 订单状态
                'or_add_time' => date('Y-m-d H:i:s'), // 添加订单时间
                'ca_id' => $ca_id,    //  1 完整套餐区 2 筛选套餐区
                'gong_id' => $gongIds, // 供货商id
                'or_num' => date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT)  //订单号
            ];

            $order_id = model('order')->insertGetId($order);  // 订单id
            if (!$order_id) {
                $this->result('', 0, '订单添加失败', 'json');
            }
//             $data['goods_id'];  //商品id (数组)
            foreach ($data['goods_id'] as $k => $v) {
                $orderDetail = $this->orderDetail($order_id, $v);
            }
            if ($orderDetail) {
                return ['code' => 1, 'msg' => '成功', 'data' => $order_id];
            }
        }



    /**
     * 插入订单详情
     * @param $orID
     * @param $id
     * @return int|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function orderDetail($orID,$id){

        $goods = model('Product')->where('id', $id)->find();
        // 订单详情
        $order_detail = [
            'or_id' => $orID,                    // 订单id
            'ca_id' => $goods['ca_id'],         // 商品分类id
            'shopping_id' =>$goods['type'],     // 商品类型、商品或者赠品
            'pd_id' => $goods['id'],            // 产品id
            'gong_id'=>$goods['gong_id'],       // 供货商id
            'gong_money'=>$goods['gong_money'],  // 供货商所得金额
            'or_pd_name' => $goods['pd_name'],   // 产品名称
//            'or_pd_pic' => $goods['pd_pic'],     // 产品图
            'or_pd_head_pic' => $goods['pd_head_pic'],     // 产品缩略图
            'or_pd_price' => $goods['pd_price'],       // 产品价格
            'or_rebate' => 0,                          //  旧电视抵扣价
            'or_pd_total' => $goods['pd_price'],        //  实际应付价格
            'or_pd_num' => 1,                   // 产品数量
            'or_pd_content' => $goods['pd_content'],            //商品描述
        ];
       return model('OrderDetail')->insertGetId($order_detail);
    }

}